<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Admin\Controller;

use Admin\Model\RoleModel;
use Admin\Model\AuthModel;
use Admin\Model\RoleAuthModel;
use Admin\Controller\AdminController;

/**
 * Description of RoleController
 *
 * @author wangnaixin
 * @todo 角色模块
 * @menu 角色管理
 * @menuAuto index
 * @order 50
 * @autoCheck index,getAuthList
 * @icon role.png
 */
class RoleController extends AdminController {
    //put your code here

    public $role;
    public $auth;
    public $roleAuth;
    public $roleId;
    
    function __construct() {
        
        parent::__construct();
        $this->role = new RoleModel();
        $this->auth = new AuthModel();
        $this->roleAuth = new RoleAuthModel();
        $this->roleId = I('roleId');
    }
    
    /**
     * 
     * @todo 角色列表
     * @menu 角色列表
     * @order 10
     */
    public function index(){
        
        $roleList = $this->role->getList();
        $this->assign("roleList",$roleList);
        $this->display();
    }
    
    public function getRoleList(){
        $roleList = $this->role->getList();
        $this->ajaxReturn($roleList);
    }


    /**
     * 
     * @auth hotterWang
     * @todo 异步获权限
     * 
     */
    
    public function getAuthList(){
        $authList = $this->auth->getAuthList();
        $enabledController = C('ENABLED_CONTROLLER');
        if (count($enabledController) == 1) {
            $module = $enabledController[0];
            foreach ($authList as $key => $authItem) {
                if ($authItem['module'] && $authItem['module'] != $module) {
                    unset($authList[$key]);
                }
            }
        }
        $roleId = I('roleId');
        $checkList = $this->roleAuth->getAuthListByRoleId($roleId);
        $list = [];
        foreach ($checkList as $v){
            $list[] = $v['auth_id'];
        }
        $role = $this->role->where(array('id' => $roleId))->find();
        $this->assign("roleId",$roleId);
        $this->assign("role",$role);
        $this->assign("authList",$authList);
        $this->assign("checkList",$list);
        $this->display("authsList");
    }
    
    /**
     * @auth hotterWang
     * @todo 异步保存权限
     */
    public function setAuths(){
        $auths = I("auths");
        $this->roleAuth->delByRoleId($this->roleId);
        $this->roleAuth->setRoleIdAuths($this->roleId,$auths);
        $this->ajaxReturn($auths);
    }
    
    /**
     * @author  hotterWang
     * @todo 添加用户组
     */
    
    public function addRole(){
        $response = array('status' => false, 'msg' => '用户组添加失败!');
        $_POST['module'] = session('default_module');
        if($this->role->create()){
            if ($this->role->checkExists($_POST['name'])) {
                $response['msg'] = '该角色名称已经存在';
                $this->ajaxReturn($response);
            }
            $rs = $this->role->add();
            if($rs !== false ){
                $response['status'] = true;
                $response['msg'] = '用户组添加成功!';
                $this->ajaxReturn($response);
            }
        }else{
            $response['msg'] = $this->role->getDbError();
            $this->ajaxReturn($response);
        }
    }
    
    /**
     * 
     * @author hotterWang
     * @todo 删除用户组
     */
    
    public function delRole(){
        $this->roleAuth->delByRoleId($this->roleId);
        $this->role->delRole($this->roleId);
        $this->ajaxReturn(true);
        
    }
    /**
     * 
     * @author hotterWang
     * @todo 修改用户组
     */
    public function editRole(){
        $roleInfo = $this->role->getRoleInfoById($this->roleId);
        $this->assign("role",$roleInfo);
        $this->display();
    }
    
    /**
     * 
     * @author hotterWang
     * @todo 修改用户组操作
     */
    public function doEditRole(){
        $response = array('status' => false, 'msg' => '用户组修改失败!');
        if($this->role->create()){

            if ($this->role->checkExists($_POST['name'],$this->roleId)) {
                $response['msg'] = '该角色名称已经存在';
                $this->ajaxReturn($response);
            }
            
            if($this->role->where("id = ".$this->roleId)->save() !== false){
                $response['status'] = true;
                $response['msg'] = '用户组修改成功!';
                $this->ajaxReturn($response);
                
            }
        }else{
            $response['msg'] = $this->role->getDbError();
            $this->ajaxReturn($response);
        }
    }

}

